﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;
using BartonExtension;
using ProtoBuf;
using ServiceStack.Redis;
using ServiceStack.Redis.Generic;
using Cache.Model;

namespace Cache.Utils
{
    public static class CacheUtils
    {
        public static T CacheGet<T>(this IRedisClient cache, string key)
        {
            if(cache.ContainsKey(key))
            {
                return cache.Get<T>(key);
            }

            return default(T);
        }

        public static CacheObject CacheGet(this IRedisClient cache, string key)
        {
            if(cache.ContainsKey(key))
            {
                return cache.Get<CacheObject>(key);
            }
            return null;
        }

        public static CachingResult<CacheObject> CacheSet(this IRedisClient cache, CacheObject cachedObject)
        {
            return cache.CacheSet<CacheObject>(cachedObject, cachedObject.cacheIdentifier);
        }
        public static CachingResult<T> CacheSet<T>(this IRedisClient cache, T cachedObject, string key)
        {
            CachingResult<T> result = new CachingResult<T>();
            bool cacheSuccessFlag = cache.Set<T>(key, cachedObject);
            result.isSuccessCached = cacheSuccessFlag;
            result.objectToCache = cachedObject;
            result.timestamp = DateTime.Now.GetTimeIntervalFrom1970WithDateTime();
            return result;
        }
        public static CachingResult<List<T>> CacheItemsToPool<T>(this IRedisClient cache, List<T> items)
        {
            CachingResult<List<T>> result = new CachingResult<List<T>>();
            result.objectToCache = items;
            result.timestamp = DateTime.Now.GetTimeIntervalFrom1970WithDateTime();
            
            IRedisTypedClient<T> typedClient = cache.As<T>();
            try
            {
                typedClient.StoreAll(items);
            }
            catch (Exception e)
            {
                result.isSuccessCached = false;
            }
            return result;
        }
        public static List<T> GetItemsFromPool<T>(this IRedisClient cache)
        {
            try
            {
                IRedisTypedClient<T> typedClient = cache.As<T>();
                return typedClient.GetAll().ToList();
            }
            catch(Exception e)
            {
                return null;
            }
        }
        public static void CleanPool<T>(this IRedisClient cache)
        {
            try
            {
                IRedisTypedClient<T> typedClient = cache.As<T>();
                typedClient.DeleteAll();
            }
            catch(Exception)
            {

            }
        }
        public static bool DeleteItemFromPool<T>(this IRedisClient cache,T item)
        {
            try
            {
                IRedisTypedClient<T> typedClient = cache.As<T>();
                typedClient.Delete(item);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
        public static bool RemoveCache(this IRedisClient cache,string key)
        {
            try
            {
                cache.DeleteById<string>(key);
                return true;
            }
            catch(Exception e)
            {
                return false;
            }
        }
    }
}
